package com.sfh.cpc.mass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cpc.R;
import com.sfh.cpc.com.entity.EMass;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.refresh.SwipeRefreshMoreLayout;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.mass.adapter.MyMassAdapter;
import com.sfh.cpc.mass.model.MassModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.lib.ui.dialog.DialogBuilder;

/**
 * 功能描述: 我的创建
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/7
 */
public class MyMassActivity extends AbstractLifecycleActivity<MassModel> implements SwipeRefreshMoreLayout.OnLoadingListener,
        BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener {


    public static void startUI(Context context) {

        Intent intent = new Intent(context, MyMassActivity.class);
        context.startActivity(intent);
    }

    WidgetBaseToolbar toolbar;
    SwipeRefreshMoreLayout layout;
    MyMassAdapter adapter;
    int nowPage = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_my_activity);

        layout = findViewById(R.id.swipeRefreshMoreLayout);
        toolbar = findViewById(R.id.toolbar);
        toolbar.addLine();
        toolbar.setTitle("我的社区");
        toolbar.setRightView(R.drawable.ysf_ic_menu_more_dark).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MassAddActivity.startUI(MyMassActivity.this);
            }
        });


        adapter = new MyMassAdapter();
        layout.setAdapter(adapter, true);
        layout.addItemDecoration(R.dimen.px_1);
        layout.setOnItemClickListener(this);
        layout.setOnItemChildClickListener(this);
        layout.setOnLoadingListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        layout.onAutoRefresh();
    }

    @Override
    public void onRefreshUI() {
        nowPage = 1;
        this.getViewModel().pageUserMass(nowPage);
    }

    @Override
    public void onLoadMoreUI() {
        nowPage++;
        this.getViewModel().pageUserMass(nowPage);
    }

    @LiveDataMatch
    public void onPageSuccess(RspPage<EMass> data) {

        if (data == null) {
            this.layout.onLoadMoreFail();

        } else {
            nowPage = data.getPageNumber();
            layout.onRefreshCompale(data.getList(), data.getPageNumber() != 1);
            if (data.getTotalPage() == data.getPageNumber()) {
                layout.onLoadAllCompale();
            }
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EMass mass = (EMass) adapter.getItem(position);
        MassActivity.startUI(this, mass);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        final EMass mass = (EMass) adapter.getItem(position);

        if (view.getId() == R.id.btEdit) {
            MassEditActivity.startUI(this, mass.getMassId());
        } else if (view.getId() == R.id.btDel) {


            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("确定删除社区吗？");
            builder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogBuilder.DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    getViewModel().delMass(mass.getMassId());
                }
            });
            showDialog(builder);
        }
    }
}
